/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 *
 * The contents of this file are subject to the terms of either the GNU
 * General Public License Version 2 only ("GPL") or the Common
 * Development and Distribution License("CDDL") (collectively, the
 * "License"). You may not use this file except in compliance with the
 * License. You can obtain a copy of the License at
 * http://www.netbeans.org/cddl-gplv2.html
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 * specific language governing permissions and limitations under the
 * License.  When distributing the software, include this License Header
 * Notice in each file and include the License file at
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Sun in the GPL Version 2 section of the License file that
 * accompanied this code. If applicable, add the following below the
 * License Header, with the fields enclosed by brackets [] replaced by
 * your own identifying information:
 * "Portions Copyrighted [year] [name of copyright owner]"
 *
 * Contributor(s):
 *
 * The Original Software is NetBeans. The Initial Developer of the Original
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
 * Microsystems, Inc. All Rights Reserved.
 *
 * If you wish your version of this file to be governed by only the CDDL
 * or only the GPL Version 2, indicate your decision by adding
 * "[Contributor] elects to include this software in this distribution
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
 * single choice of license, a recipient has the option to distribute
 * your version of this file under either the CDDL, the GPL Version 2 or
 * to extend the choice of license to its licensees as provided above.
 * However, if you add GPL Version 2 code and therefore, elected the GPL
 * Version 2 license, then the option applies only if the new code is
 * made subject to such option by the copyright holder.
 */
package ch.simas.nbtostring.ui;
import ch.simas.nbtostring.util.Problem;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import javax.swing.ImageIcon;
import javax.swing.JPanel;


/**
 *
 * @author  Jan Becicka
 */
public class ErrorPanel extends javax.swing.JPanel {
    
    private static final long serialVersionUID = 7393495635394839195L;

    private static ImageIcon fatalImage = null, nonFatalImage = null;

    /** Creates new form ErrorPanel */
    public ErrorPanel() {
        initComponents();
        headLine.setBackground(javax.swing.UIManager.getDefaults().getColor("Panel.background")); // NOI18N
        setPreferredSize(new Dimension(510, 200));
    }
    
    /**
     * Initializes this panel with a problem.
     * @param problem the problem to initialize with
     * @see #setProblems(Problem) 
     */
    public ErrorPanel(final Problem problem) {
        this();
        setProblems(problem);
    }
    
    /**
     * Adds a problem into this. This will be the new root problem.
     * @param problem the problem to add
     */
    public void setProblems(Problem problem) {
        errors.removeAll();
        int i = 0;
        while (problem != null) {
            GridBagConstraints gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.gridx = 0;
            gridBagConstraints.gridy = i++;
            gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
            gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
            gridBagConstraints.weightx = 1.0;
            
            ProblemComponent c = new ProblemComponent(problem);
            errors.add(c, gridBagConstraints);
            
            problem = problem.getNext();
            
            if (i%2 == 0) {
                c.setLightBackground();
            } else {
                c.setDarkBackground();
            }
        }

        GridBagConstraints gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = i;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        
        JPanel jp = new JPanel();
        jp.setBackground(Color.WHITE);
        errors.add(jp, gridBagConstraints);
    }

    /**
     * Gets the image for a fatal error.
     * @return the image for a fatal error
     */
    static ImageIcon getFatalErrorIcon() {
        if (fatalImage == null) {
            fatalImage = new ImageIcon(ErrorPanel.class.getResource("/ch/simas/nbtostring/ui/resources/error.png")); //NOI18N
        }
        return fatalImage;
    }

    /**
     * Gets the image to indicate a "problem" only.
     * @return the image indicating a problem
     */
    static ImageIcon getNonfatalErrorIcon() {
        if (nonFatalImage == null) {
            nonFatalImage = new ImageIcon(ErrorPanel.class.getResource("/ch/simas/nbtostring/ui/resources/warning.png")); //NOI18N
        }
        return nonFatalImage;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    listPanel = new javax.swing.JPanel();
    errorLabel = new javax.swing.JLabel();
    listScrollPane = new javax.swing.JScrollPane();
    errors = new javax.swing.JPanel();
    explanationPanel = new javax.swing.JPanel();
    fatalError = new javax.swing.JLabel();
    nonFatalError = new javax.swing.JLabel();
    headLine = new javax.swing.JTextArea();

    setLayout(new java.awt.BorderLayout());

    listPanel.setLayout(new java.awt.BorderLayout());

    errorLabel.setLabelFor(errors);
    org.openide.awt.Mnemonics.setLocalizedText(errorLabel, org.openide.util.NbBundle.getBundle("ch/simas/nbtostring/ui/Bundle").getString("LBL_ErrorsList")); // NOI18N
    listPanel.add(errorLabel, java.awt.BorderLayout.NORTH);

    listScrollPane.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

    errors.setLayout(new java.awt.GridBagLayout());
    listScrollPane.setViewportView(errors);

    listPanel.add(listScrollPane, java.awt.BorderLayout.CENTER);

    add(listPanel, java.awt.BorderLayout.CENTER);

    explanationPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

    fatalError.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ch/simas/nbtostring/ui/resources/error.png"))); // NOI18N
    org.openide.awt.Mnemonics.setLocalizedText(fatalError, org.openide.util.NbBundle.getBundle("ch/simas/nbtostring/ui/Bundle").getString("LBL_FatalError")); // NOI18N
    fatalError.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 20));
    explanationPanel.add(fatalError);

    nonFatalError.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ch/simas/nbtostring/ui/resources/warning.png"))); // NOI18N
    org.openide.awt.Mnemonics.setLocalizedText(nonFatalError, org.openide.util.NbBundle.getBundle("ch/simas/nbtostring/ui/Bundle").getString("LBL_NonFatalError")); // NOI18N
    explanationPanel.add(nonFatalError);

    add(explanationPanel, java.awt.BorderLayout.SOUTH);

    headLine.setBackground(javax.swing.UIManager.getDefaults().getColor("Panel.background"));
    headLine.setEditable(false);
    headLine.setFont(errorLabel.getFont());
    headLine.setLineWrap(true);
    headLine.setText(org.openide.util.NbBundle.getBundle("ch/simas/nbtostring/ui/Bundle").getString("LBL_ErrorPanelDescription")); // NOI18N
    headLine.setWrapStyleWord(true);
    headLine.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 10, 1));
    add(headLine, java.awt.BorderLayout.NORTH);
    java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("ch/simas/nbtostring/ui/Bundle"); // NOI18N
    headLine.getAccessibleContext().setAccessibleName(bundle.getString("ACSD_ErrorPanelName")); // NOI18N
    headLine.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(ErrorPanel.class, "ACSD_ErrorPanelDescription")); // NOI18N

    getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getBundle(ErrorPanel.class).getString("ACSD_ErrorPanelName")); // NOI18N
    getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(ErrorPanel.class).getString("ACSD_ErrorPanelDescription")); // NOI18N
  }// </editor-fold>//GEN-END:initComponents
    
    
  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JLabel errorLabel;
  private javax.swing.JPanel errors;
  private javax.swing.JPanel explanationPanel;
  private javax.swing.JLabel fatalError;
  private javax.swing.JTextArea headLine;
  private javax.swing.JPanel listPanel;
  private javax.swing.JScrollPane listScrollPane;
  private javax.swing.JLabel nonFatalError;
  // End of variables declaration//GEN-END:variables
    
}
